package com.taotao.search.service;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.ExceptionUtil;
import com.taotao.common.TaotaoResult;
import com.taotao.search.mapper.ItemMapper;
import com.taotao.search.pojo.Item;
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private SolrServer solrServer;
	@Override
	public TaotaoResult importAllItems() {
		try {
			List<Item> items=itemMapper.getItemList();
			for (Item item : items) {
				SolrInputDocument sid=new SolrInputDocument();
				sid.setField("id", item.getId());
				sid.setField("item_title", item.getTitle());
				sid.setField("item_sell_point", item.getSell_point());
				sid.setField("item_price", item.getPrice());
				sid.setField("item_image", item.getImage());
				sid.setField("item_category_name", item.getCategory_name());
				sid.setField("item_desc", item.getItem_des());
				//写入索引库
				solrServer.add(sid);
			}
			//提交修改
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		} 
		return TaotaoResult.ok();
	}

}
